package com.xbongbong.pay.platform.alipay.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 *
 * @author 魏荣杰
 * @date 2021/1/25 18:51
 * @since
 * @version
 */
@Configuration
@Conditional(AliPayConfigCondition.class)
@EnableConfigurationProperties(AliPayConfig.class)
public class AliPayMetaConfiguration {
    private static final Logger LOG = LoggerFactory.getLogger(AliPayMetaConfiguration.class);

    @Bean
    @ConditionalOnMissingBean
    public AliPayMetaContainer aliPayMetaContainer(AliPayConfig aliPayConfig) {
        AliPayMetaContainer aliPayMetaContainer = new AliPayMetaContainer();
        Map<String, AliPayConfig.Config> config = aliPayConfig.getConfig();
        config.forEach((k, v) -> {
            try {
                AliPayMeta aliPayMeta = new AliPayMeta(v);
                aliPayMeta.setMchId(k);
                aliPayMetaContainer.addAliPayMeta(aliPayMeta);
            } catch (Exception e) {
                LOG.error("com.xbongbong.pay.platform.alipay.config.AliPayMetaConfiguration.aliPayMetaContainer error", e);
            }

        });
        return aliPayMetaContainer;
    }
}
